package com.tgpms.web.assets.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.util.PageView;
import com.tgpms.util.VformBeanUtil;
import com.tgpms.web.assets.dao.FixedAssetMapper;
import com.tgpms.web.assets.entity.FixedAsset;
import com.tgpms.web.assets.service.FixedAssetService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class FixedAssetServiceImpl implements FixedAssetService {

	@Autowired
	private VformInterface vformInterface;

	/**
	 * @Author newway
	 * @Description 查询固定资产登记信息分页  code="SCWZC021"
	 * @Date 2019年8月2日下午4:22:23
	 * @Param com.tgpms.util.PageView
	 * @return com.tgpms.util.PageView
	 **/
	@Override
	public Result selFixedAssetList(PageInfo pageInfo) {
		JSONObject param = new JSONObject();
		Object selparam = pageInfo.getQueryMap().get("selparam");
		Object leftdition = pageInfo.getQueryMap().get("leftdition");
		Object condition = pageInfo.getQueryMap().get("condition");
		Object dataowner = pageInfo.getQueryMap().get("dataowner");
		if(Common.isEmpty(selparam)) {
			param.put("[SQL1]", " ");
		}else {
			param.put("[SQL1]", selparam.toString());
		}
		if(Common.isEmpty(leftdition)) {
			param.put("[SQL2]", " ");
		}else {
			param.put("[SQL2]", leftdition.toString());
		}
		if(Common.isEmpty(condition)) {
			param.put("[SQL3]", " ");
		}else {
			param.put("[SQL3]", condition.toString());
		}
		Result result = vformInterface.multiForMsQueryBySql("SCWZC021", param.toJSONString(), pageInfo.getPage(), 
				pageInfo.getSize(), "Y", "Y", dataowner.toString());
		return result;
	}

	/**
	 * @Author newway
	 * @Description 添加固定资产登记接口信息
	 * @Date 2019年8月2日下午4:32:56
	 * @Param com.tgpms.web.assets.entity.FixedAsset
	 * @return Integer
	 **/
	@SuppressWarnings("unchecked")
	@Override
	public synchronized Map<String,String> addFixedAssetInfo(FixedAsset fixedAsset) {
		Map<String,String> map = new HashMap<String, String>();
		//查询vform数据库获取最大主键值
		String fixedAssetNo = fixedAsset.getFixedAssetCode()+fixedAsset.getManageUnit();
		JSONObject param = new JSONObject();
		param.put("[SQL1]", " ");
		param.put("[SQL2]", " ");
		param.put("[SQL3]", " and t.FIXED_ASSET_CODE='"+fixedAsset.getFixedAssetCode()
		+"' and t.MANAGE_UNIT='"+fixedAsset.getManageUnit()+"' order by t.FIXED_ASSET_NO desc");
		Result result = vformInterface.multiForMsQueryBySql("SCWZC021", param.toJSONString(), "1", 
				"1", "N", null, null);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject resJson = JSONObject.parseObject(resData);
		String resList = resJson.getString("rows");
		if(resData.equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		if(resData.equals("{\"total\":\"0\",\"rows\":[]}")) {
			fixedAssetNo = fixedAssetNo+"0001";
		}else {
			List<Map<String,String>> list = JSONArray.parseObject(resList, List.class);
			String fixno = list.get(0).get("FIXEDASSETNO");
			String max = fixno.substring(fixno.length()-4, fixno.length());
			fixedAssetNo = fixedAssetNo+String.format("%04d",Integer.parseInt(max)+1);
		}
		System.out.println("得到主键为:  "+fixedAssetNo);
		fixedAsset.setFixedAssetNo(fixedAssetNo);
		//对象转json字符串, 字段驼峰转下划线大写字段
		JSONObject json = VformBeanUtil.entityToJsonString(fixedAsset);
		if(json==null || json.isEmpty()) {
			map.put("code", "-2");
			map.put("info", ExceptionExplain.ERROR_BY_PARSING.getExplain());
			return map;
		}
		System.out.println("vform增加上送参数   "+json.toString());
		//vform同步插入
		result = vformInterface.insert("D8001", json);
		System.out.println("vform增加返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		resData = result.getData().toString();
		if(resData.equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}
		else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			map.put("data", fixedAsset.getFixedAssetNo());
		}
		return map;
	}

	/**
	 * @Author newway
	 * @Description 更新固定资产登记接口信息
	 * @Date 2019年8月2日下午4:37:30
	 * @Param com.tgpms.web.assets.entity.FixedAsset
	 * @return Integer
	 **/
	@Override
	public Map<String,String> updateFixedAssetInfo(FixedAsset fixedAsset) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("FIXED_ASSET_NO", fixedAsset.getFixedAssetNo());
		JSONObject pajson = new JSONObject();
		pajson = VformBeanUtil.entityToJsonStringByUpdate(fixedAsset);
		pajson.remove("FIXED_ASSET_NO");

		System.out.println("vform更新上送主键   "+keyjson.toString());
		System.out.println("vform更新上送参数   "+pajson.toString());
		//vform同步更新
		Result result = vformInterface.update("D8001", keyjson.toJSONString(),pajson.toJSONString(),"Y","Y",fixedAsset.getDataOwner());
		System.out.println("vform更新返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		if(result.getData().toString().equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}
		return map;
	}

	/**
	 * @Author newway
	 * @Description 删除固定资产登记接口信息
	 * @Date 2019年8月2日下午4:42:04
	 * @Param fixedAssetNo
	 * @return Integer
	 **/
	@Override
	public Map<String,String> deleteFixedAssetInfo(String fixedAssetNo) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("FIXED_ASSET_NO", fixedAssetNo);
		System.out.println("vform删除上送主键   "+keyjson.toString());
		//vform同步删除
		Result result = vformInterface.delete("D8001", keyjson.toJSONString());
		System.out.println("vform删除返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		if(result.getData().toString().equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}
		return map;
	}


}
